#!/bin/bash

if [ -z "$*" ]; then
echo ""
echo "Usage: popcrun objects.config [-drun] [-runlocal] prog.main args..."
echo "  -drun     : Print launching command only."
echo "  -gdb      : Run with gdb."
echo "  -runlocal : Force to create all objects locally : do not use JobMgr"
echo "  -debug    : Give some debugging informations"
echo "  -log=<filename> : put all message in the <filename> file"
echo "  -version	: Display the installed POP-C++ version"
echo ""
exit
fi

# unset POPC_DEBUG

if [ -z "$POPC_LOCATION" ]; then
POPC_LOCATION=@CMAKE_INSTALL_PREFIX@
export POPC_LOCATION
fi

. $POPC_LOCATION/etc/popc-runtime-env.sh

MYJOBSERVICE="`hostname`:2711"

#if [ -z "$POPC_JOBSERVICE" ]; then
#POPC_JOBSERVICE="`hostname`:2711"
#fi



MYAPPSERVICE=$POPC_LOCATION/services/appservice

drun=0
gdb=0
noproxy=0

logging=0
debug=0
version=0

args=""

CODECONF=""
PROG=""
localFlag=""

for arg in $* ; do
    if [ "$arg" = "-version" ]; then
		version=1
    elif [ "$arg" = "-drun" ]; then
		drun=1
    elif [ "$arg" = "-gdb" ]; then
		gdb=1
    elif [ "$arg" = "-runlocal" ]; then
		localFlag="-runlocal"
    elif [ "$arg" = "-noproxy" ]; then
		noproxy=1
	# elif [ "$arg" = "-debug" ]; then
		# export POPC_DEBUG="Y"
	elif [ `echo "$arg"|grep "^\-log="` ]; then
		export LOGGING_FILE=`echo $arg|sed s/-log=//`
		logging=1
    elif [ `echo "$arg"|grep "^\-batch="` ]; then
		export POPC_BATCH=`echo $arg|sed s/-batch=//`
    elif [ -z "$CODECONF" ]; then
		CODECONF=$arg
    elif [ -z "$PROG" ]; then
		PROG=$arg
    else
		args="$args $arg"
    fi
done

# display installed version of POP-C++ if asked

if [ $version -eq 1 ]; then
	popcc -version
	exit
fi

# if logging make some check (file exists, file is writable ...)

if [ $logging = 1 ]; then
	if [ $logging = "1" ] && [ -f $LOGGING_FILE ]; then
		read -p "The file already exists ! Do you want overwrite it ? (y/N)" yn
		if [ $yn != "y" ] && [ $yn != "Y" ]; then
			exit
		fi
		if [ ! -w $LOGGING_FILE ]; then
			echo "The file $LOGGING_FILE cannot be written !!!"
			exit
		fi
		logging_cmd=" tee $LOGGING_FILE"
	else
		touch $LOGGING_FILE
		if [ $? != 0 ]; then
			echo "The file $LOGGING_FILE cannot be created !!!"
			exit
		fi
		logging_cmd=" tee $LOGGING_FILE"
	fi
fi

#Check and dowload object map file....
if [ -z "$CODECONF" ]; then
echo "Code config file missing"
exit 1
fi

httpfile=`echo "$CODECONF" | grep http://`
ftpfile=`echo "$CODECONF" | grep ftp://`
TMPDIR=/tmp

if [ -n "$httpfile$ftpfile" ]; then
    wget=`which wget`
    if [ -z "$wget" ]; then
	echo "ERROR: wget command not found"
	exit 1
    fi
    echo Downloading $CODECONF
    LOCALCODECONF=$TMPDIR/`whoami`_`echo $CODECONF | tr "://~" "____"`
    $wget -q $CODECONF -O $LOCALCODECONF
    RETVAL=$?
    if [ $RETVAL -ne 0 ]; then
	echo "Can not get the code configuration file. WGET failed"
	exit $RETVAL
    fi
    CODECONF=$LOCALCODECONF
fi

if [ ! -f "$CODECONF" ]; then
echo "Code config file not found"
exit 1
fi

FILECONF=`basename $CODECONF`
OLDPATH=`pwd`
cd `dirname $CODECONF`
PATHCONF=`pwd`
cd $OLDPATH

if [ -z "$FILECONF" ]; then
echo "Code config file not found"
exit 1
fi


if [ -z "$PROG" ]; then
echo "Program file missing"
exit 1
fi

#Download the program if it is on the web server
httpfile=`echo "$PROG" | grep http://`
ftpfile=`echo "$PROG" | grep ftp://`

if [ -n "$httpfile$ftpfile" ]; then
    wget=`which wget`
    if [ -z "$wget" ]; then
	echo "ERROR: wget is required"
	exit 1
    fi
    echo "Downloading $PROG..."
    LOCALPROG=$TMPDIR/`whoami`_`echo $PROG | tr "://~" "____"`
    $wget -q $PROG -O $LOCALPROG
    RETVAL=$?
    if [ $RETVAL -ne 0 ]; then
	echo "Can not get the main program file. WGET failed"
	exit $RETVAL
    fi
    PROG=$LOCALPROG
    chmod +x $PROG
fi



APPCONTACT=""

MY_PROXY=""

#TMPCHALLENGE="`/usr/bin/whoami`@`hostname` on `date`"
#TMPCHALLENGE="`whoami`@`hostname` on `date`"
TMPNAME=""

if [ -n "$POPC_PROXY" ] && [ $noproxy -eq 0 ] ; then
MY_PROXY=" -proxy=$POPC_PROXY"
fi

#if [ ! -x $MYAPPSERVICE ]; then
#  echo "$MYAPPSERVICE not found"
#  exit 1
#fi
#Use the following line to run the interface side in valgrind
#cmd="valgrind --log-file=/tmp/$$ $PROG $args -initparoc -appservicecode=${MYAPPSERVICE} ${MY_PROXY} -codeconf=${PATHCONF}/${FILECONF} -jobservice=${MYJOBSERVICE} ${localFlag}"
cmd="$PROG $args -initparoc -appservicecode=$MYAPPSERVICE $MY_PROXY -codeconf=$PATHCONF/$FILECONF -jobservice=$MYJOBSERVICE $localFlag"

retval=0
if [ $drun -eq 1 ]; then
    echo "$cmd $logging_cmd"
elif [ $gdb -eq 1 ]; then
    # execute command
    if [ "$logging_cmd" != "" ]; then
        gdb --args $cmd | $logging_cmd
    else
        gdb --args $cmd
    fi
    retval=$?
else
    # execute command
    if [ "$logging_cmd" != "" ]; then
        $cmd | $logging_cmd
    else
        $cmd
    fi
    retval=$?
fi
exit $retval
